#include "pukerclass.h"
#include<QMessageBox>
pukerclass::pukerclass(void){}
pukerclass::pukerclass(int a,int b)
{
number=a;
type=b;//红心=1，方块=2，黑桃=3，梅花=4
}
 int pukerclass::samecolor(pukerclass * container){
     int blossom=0;//梅花有几张
     int heart=0;//红心
     int block=0;//方块
     int peach=0;//黑桃
     int judge[7];
     int m=0;
     int n=0;
     for(int i=0;i<7;i++){
         if((container+i)->type==1)
             heart++;
         if((container+i)->type==2)
             block++;
         if((container+i)->type==3)
             peach++;
         if((container+i)->type==4)
             blossom++;
     }
     if(heart>=5){
         for(int i=0;i<7;i++){
             if((container+i)->type==1){
                 judge[m]=(container+i)->number;
                 m++;
             }
         }
         for(int i=0;i<m;i++){
             for(int j=0;j<m-i;j++){
           if(judge[j]<judge[j+1]){
               int temp=judge[j+1];
               judge[j+1]=judge[j];
               judge[j]=temp;
             }
             }
         }
             return 600+judge[0];
     }
     if(block>=5){
         for(int i=0;i<7;i++){
             if((container+i)->type==2){
                 judge[m]=i;
                 m++;
             }
         }
         for(int i=0;i<m;i++){
             for(int j=0;j<m-i;j++){
           if(judge[j]<judge[j+1]){
               int temp=judge[j+1];
               judge[j+1]=judge[j];
               judge[j]=temp;
             }
             }
         }
             return 600+judge[0];
     }
     if(peach>=5){
         for(int i=0;i<7;i++){
             if((container+i)->type==3){
                 judge[m]=i;
                 m++;
             }
         }
         for(int i=0;i<m;i++){
             for(int j=0;j<m-i;j++){
           if(judge[j]<judge[j+1]){
               int temp=judge[j+1];
               judge[j+1]=judge[j];
               judge[j]=temp;
             }
             }
         }
             return 600+judge[0];
     }
     if(blossom>=5){
         for(int i=0;i<7;i++){
             if((container+i)->type==4){
                 judge[m]=i;
                 m++;
             }
         }
         for(int i=0;i<m;i++){
             for(int j=0;j<m-i;j++){
           if(judge[j]<judge[j+1]){
               int temp=judge[j+1];
               judge[j+1]=judge[j];
               judge[j]=temp;
             }
         }
     }
         return 600+judge[0];
     }
     else
         return 0;
 }
 int pukerclass::shunzi (pukerclass * container){
     for(int i=0;i<7;i++){
         for(int j=0;j<7-i;j++){
             if((container+j)->number<(container+j+1)->number){}
             int temp=(container+j+1)->number;
             int temp2=(container+j+1)->type;
             (container+j+1)->number=(container+j)->number;
             (container+j+1)->type=(container+j)->type;
             (container+j)->number=temp;
             (container+j)->type=temp2;
         }
     }
for(int i=1;i<5;i++){
    int w=0;
    if((container+i)->number==(container+i-1)->number-1){
        w++;
    }
    else
        break;
    while(i==4){
        if(w==4)
            return 500+(container)->number;
    }
 }
for(int i=2;i<6;i++){
    int w=0;
    if((container+i)->number==(container+i-1)->number-1){
        w++;
    }
    else
        break;
    while(i==4){
        if(w==4)
            return 500+(container+1)->number;
    }
 }
for(int i=3;i<7;i++){
    int w=0;
    if((container+i)->number==(container+i-1)->number-1){
        w++;
    }
    else
        break;
    while(i==4){
        if(w==4)
            return 500+(container+2)->number;
    }
 }
return 0;
 }
int pukerclass::samecolorshun(pukerclass *container){
    int qa2;//判断条件，同花顺需要同时满足同花和顺子
    qa2=shunzi(container);
    if(qa2/500==1){
        int answer=qa2-500;
        if(answer==14)
            return 0;
        int pt=0;
        for(int i=0;i<3;i++){
            if((container+i)->number==answer)
                pt=i;
        }
        for(int i=1;i<5;i++){
            if((container+pt+i)->type!=(container+pt)->type)
                return 0;
        }
        return 900+answer;
    }
    else
        return 0;
}
int pukerclass::royalsamecolorshun(pukerclass *container){
    int qa2;//判断条件，同花顺需要同时满足同花和顺子
    qa2=shunzi(container);
    if(qa2/500==1){
        int answer=qa2-500;
       if(answer!=14)
           return 0;
        int pt=0;
        for(int i=0;i<3;i++){
            if((container+i)->number==answer)
                pt=i;
        }
        for(int i=1;i<5;i++){
            if((container+pt+i)->type!=(container+pt)->type)
                return 0;
        }
        return 1000;
    }
    else
        return 0;
}
int pukerclass::fourcard(pukerclass *container){
    for(int i=0;i<7;i++){
        for(int j=0;j<7-i;j++){
            if((container+j)->number<(container+j+1)->number){}
            int temp=(container+j+1)->number;
            int temp2=(container+j+1)->type;
            (container+j+1)->number=(container+j)->number;
            (container+j+1)->type=(container+j)->type;
            (container+j)->number=temp;
            (container+j)->type=temp2;
        }
    }
    for(int i=0;i<4;i++){
        int w=0;
        for(int j=1;j>4;j++){
            if((container+i+j)->number==(container+i)->number)
                w++;
            if(w==3)
                return 800+(container+i)->number;
        }
    }
    return 0;
}
int pukerclass::threecard(pukerclass *container){
    int qa1;
    qa1=fourcard(container);
    if(qa1/800==1)
        return 0;
    else{
        for(int i=0;i<5;i++){
            int w=0;
        for(int j=1;j<3;j++){
            if((container+i+j)->number==(container+i)->number)
                w++;
            if(w==2){
                return 400+(container+i)->number;
          }
        }
        }
        }
    return 0;
}
double pukerclass::hulu(pukerclass *container){
    int qa2;
    qa2=threecard(container);
    if(qa2==0)
        return 0;
    else{
        for(int i=0;i<6;i++){
            if((container+i)->number==(container+i+1)->number&&(container+i)->number!=qa2-400){
                return 700+qa2-400+0.01*((container+i)->number);
            }
        }
        return 0;
    }
}
int pukerclass::onepair(pukerclass *container){
    int qa1,qa2;
    qa1=fourcard(container);
    qa2=threecard(container);
    if(qa2!=0||qa1!=0)
        return 0;
    else{
        for(int i=0;i<6;i++){
            if((container+i)->number==(container+i+1)->number)
                return 200+(container+i)->number;
        }
        return 0;
    }
}
double pukerclass::twopair(pukerclass *container){
    int qa1=onepair(container);
    if(qa1==0)
        return 0;
    else{
        for(int i=0;i<6;i++){
             if((container+i)->number==(container+i+1)->number&&(container+i)->number!=qa1-200)
                 return 300+qa1-200+0.01*((container+i)->number);
        }
        return 0;
    }
}
int pukerclass::highpuker(pukerclass *container){
    int qa1,qa2,qa3,qa4,qa5;
    qa1=fourcard(container);
    qa2=threecard(container);
    qa3=shunzi(container);
    qa4=samecolor(container);
    qa5=onepair(container);
    if(qa1!=0||qa2!=0||qa3!=0||qa4!=0||qa5!=0)
        return 0;
    else{
        if((container)->number==14)
            return 100+(container+1)->number;
        else
            return 0;
    }
}
double pukerclass::getthehighest(pukerclass *container){
    int thehighest=0;//最高的数码
    thehighest=samecolor(container);
    if(shunzi(container)>thehighest)
        thehighest=shunzi(container);
    if(samecolorshun(container)>thehighest)
               thehighest=samecolorshun(container);
    if(royalsamecolorshun(container)>thehighest)
        thehighest=royalsamecolorshun(container);
    if(fourcard(container)>thehighest)
        thehighest=fourcard(container);
    if(threecard(container)>thehighest)
        thehighest=threecard(container);
    if(hulu(container)>thehighest)
        thehighest=hulu(container);
    if(onepair(container)>thehighest)
        thehighest=onepair(container);
    if(twopair(container)>thehighest)
        thehighest=twopair(container);
    if(highpuker(container)>thehighest)
        thehighest=highpuker(container);
    return thehighest;
}
void pukerclass::contrast(pukerclass * con1,pukerclass * con2){
    if(getthehighest(con1)>getthehighest(con2))
        QMessageBox::information(NULL,"GAME OVER","对家获胜",QMessageBox::Close);
    else{
        QMessageBox::information(NULL,"GAME OVER","本家获胜",QMessageBox::Close);
    }
}


